package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传文件
 * @author lixu
 */
@RequestMapping("/admin/storage")
@RestController
@CrossOrigin
public class StorageController {


    @Autowired
    private DtsStorageService dtsStorageService;

    /**
     * 上传图片
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"id": 34,
     * 		"key": "dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg",
     * 		"name": "16.jpg",
     * 		"type": ".jpg",
     * 		"size": 35012,
     * 		"url": "http://qianfeng-file.oss-cn-beijing.aliyuncs.com/dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg?Expires=1622471984&OSSAccessKeyId=LTAI4GCjGC58aGgnLjd5hixp&Signature=%2Bu%2BczJdE4I6jW4DagLVYT0Ps3dw%3D",
     * 		"addTime": 1621391984382,
     * 		"updateTime": 1621391984382,
     * 		"deleted": false
     *        },
     * 	"errmsg": "成功"
     * }
     */
    @RequiresPermissions(value = "admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "添加")
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception{

        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //开始上传
        DtsStorage dtsStorage = dtsStorageService.create(
                ext,file.getSize(),file.getBytes());
        return ResponseUtil.ok(dtsStorage);
    }


}
